{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 1,
   "id": "48a2a5d7",
   "metadata": {},
   "outputs": [],
   "source": [
    "import numpy as np\n",
    "import pandas as pd\n",
    "import matplotlib.pyplot as plt\n",
    "from sklearn.datasets import load_iris\n",
    "from sklearn.preprocessing import StandardScaler\n",
    "from sklearn.model_selection import train_test_split,cross_val_score,GridSearchCV\n",
    "from sklearn.linear_model import LogisticRegression"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "bb149ba3",
   "metadata": {},
   "source": [
    "### 导入数据"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 21,
   "id": "1e32b7b8",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>sepal length (cm)</th>\n",
       "      <th>sepal width (cm)</th>\n",
       "      <th>petal length (cm)</th>\n",
       "      <th>petal width (cm)</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>0</th>\n",
       "      <td>5.1</td>\n",
       "      <td>3.5</td>\n",
       "      <td>1.4</td>\n",
       "      <td>0.2</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1</th>\n",
       "      <td>4.9</td>\n",
       "      <td>3.0</td>\n",
       "      <td>1.4</td>\n",
       "      <td>0.2</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2</th>\n",
       "      <td>4.7</td>\n",
       "      <td>3.2</td>\n",
       "      <td>1.3</td>\n",
       "      <td>0.2</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>3</th>\n",
       "      <td>4.6</td>\n",
       "      <td>3.1</td>\n",
       "      <td>1.5</td>\n",
       "      <td>0.2</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>4</th>\n",
       "      <td>5.0</td>\n",
       "      <td>3.6</td>\n",
       "      <td>1.4</td>\n",
       "      <td>0.2</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>...</th>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>145</th>\n",
       "      <td>6.7</td>\n",
       "      <td>3.0</td>\n",
       "      <td>5.2</td>\n",
       "      <td>2.3</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>146</th>\n",
       "      <td>6.3</td>\n",
       "      <td>2.5</td>\n",
       "      <td>5.0</td>\n",
       "      <td>1.9</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>147</th>\n",
       "      <td>6.5</td>\n",
       "      <td>3.0</td>\n",
       "      <td>5.2</td>\n",
       "      <td>2.0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>148</th>\n",
       "      <td>6.2</td>\n",
       "      <td>3.4</td>\n",
       "      <td>5.4</td>\n",
       "      <td>2.3</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>149</th>\n",
       "      <td>5.9</td>\n",
       "      <td>3.0</td>\n",
       "      <td>5.1</td>\n",
       "      <td>1.8</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "<p>150 rows × 4 columns</p>\n",
       "</div>"
      ],
      "text/plain": [
       "     sepal length (cm)  sepal width (cm)  petal length (cm)  petal width (cm)\n",
       "0                  5.1               3.5                1.4               0.2\n",
       "1                  4.9               3.0                1.4               0.2\n",
       "2                  4.7               3.2                1.3               0.2\n",
       "3                  4.6               3.1                1.5               0.2\n",
       "4                  5.0               3.6                1.4               0.2\n",
       "..                 ...               ...                ...               ...\n",
       "145                6.7               3.0                5.2               2.3\n",
       "146                6.3               2.5                5.0               1.9\n",
       "147                6.5               3.0                5.2               2.0\n",
       "148                6.2               3.4                5.4               2.3\n",
       "149                5.9               3.0                5.1               1.8\n",
       "\n",
       "[150 rows x 4 columns]"
      ]
     },
     "execution_count": 21,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "x = pd.DataFrame(data=load_iris().data,columns=load_iris().feature_names)\n",
    "x"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 15,
   "id": "7fd88053",
   "metadata": {
    "collapsed": true
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,\n",
       "       0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,\n",
       "       0, 0, 0, 0, 0, 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,\n",
       "       1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,\n",
       "       1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2,\n",
       "       2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2,\n",
       "       2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2])"
      ]
     },
     "execution_count": 15,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "y = load_iris().target\n",
    "y"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "2ba2a527",
   "metadata": {},
   "source": [
    "### 切分数据集"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 16,
   "id": "81100f83",
   "metadata": {},
   "outputs": [],
   "source": [
    "xtrain,xtest,ytrain,ytest = train_test_split(x,y,test_size=0.3,random_state=420)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "88c4262b",
   "metadata": {},
   "source": [
    "### 使用标准化包，对训练集来学习，从而对训练集和测试集来做标准化"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 18,
   "id": "38dfb3c9",
   "metadata": {
    "collapsed": true
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([[ 6.98926548e-01,  3.11391996e-01,  7.07721645e-01,\n",
       "         9.95825472e-01],\n",
       "       [ 4.65951032e-01, -1.67688798e+00,  3.19241130e-01,\n",
       "         8.77215987e-02],\n",
       "       [-1.16487758e+00,  9.04719988e-02, -1.29017814e+00,\n",
       "        -1.33929877e+00],\n",
       "       [ 1.51434085e+00,  3.11391996e-01,  1.20719659e+00,\n",
       "         7.36367223e-01],\n",
       "       [-1.16487758e+00, -1.30447998e-01, -1.34567536e+00,\n",
       "        -1.33929877e+00],\n",
       "       [ 3.49463274e-01,  7.53231990e-01,  8.74213294e-01,\n",
       "         1.38501285e+00],\n",
       "       [-4.65951032e-01,  9.74151987e-01, -1.40117258e+00,\n",
       "        -1.33929877e+00],\n",
       "       [-9.31902064e-01,  1.63691198e+00, -1.29017814e+00,\n",
       "        -1.20956965e+00],\n",
       "       [-1.16487758e-01, -5.72287992e-01,  7.07721645e-01,\n",
       "         1.51474197e+00],\n",
       "       [ 2.32975516e-01, -1.30447998e-01,  5.96727212e-01,\n",
       "         7.36367223e-01],\n",
       "       [-8.15414306e-01,  2.29967197e+00, -1.29017814e+00,\n",
       "        -1.46902790e+00],\n",
       "       [ 2.32975516e+00,  1.63691198e+00,  1.42918546e+00,\n",
       "         9.95825472e-01],\n",
       "       [ 1.28136534e+00,  3.11391996e-01,  4.85732779e-01,\n",
       "         2.17450723e-01],\n",
       "       [ 1.04838982e+00,  3.11391996e-01,  1.15169938e+00,\n",
       "         1.38501285e+00],\n",
       "       [-3.49463274e-01, -1.23504798e+00,  4.17550485e-02,\n",
       "        -1.71736651e-01],\n",
       "       [ 2.32975516e-01, -5.72287992e-01,  9.72522648e-02,\n",
       "         8.77215987e-02],\n",
       "       [ 9.31902064e-01,  5.32311993e-01,  1.04070494e+00,\n",
       "         1.64447110e+00],\n",
       "       [-1.28136534e+00, -1.30447998e-01, -1.34567536e+00,\n",
       "        -1.20956965e+00],\n",
       "       [-1.28136534e+00,  7.53231990e-01, -1.06818928e+00,\n",
       "        -1.33929877e+00],\n",
       "       [-2.32975516e-01, -1.23504798e+00,  6.52224428e-01,\n",
       "         9.95825472e-01],\n",
       "       [ 2.09677965e+00, -5.72287992e-01,  1.59567711e+00,\n",
       "         9.95825472e-01],\n",
       "       [ 1.16487758e+00,  9.04719988e-02,  7.07721645e-01,\n",
       "         1.38501285e+00],\n",
       "       [ 1.39785310e+00, -1.30447998e-01,  1.15169938e+00,\n",
       "         1.12555460e+00],\n",
       "       [-9.31902064e-01,  9.74151987e-01, -1.34567536e+00,\n",
       "        -1.33929877e+00],\n",
       "       [-1.86380413e+00, -1.30447998e-01, -1.51216701e+00,\n",
       "        -1.46902790e+00],\n",
       "       [-2.32975516e-01,  1.63691198e+00, -1.17918371e+00,\n",
       "        -1.20956965e+00],\n",
       "       [-9.31902064e-01,  1.41599198e+00, -1.29017814e+00,\n",
       "        -1.07984052e+00],\n",
       "       [ 1.16487758e-01, -1.89780797e+00,  6.52224428e-01,\n",
       "         3.47179848e-01],\n",
       "       [-1.04838982e+00,  1.19507198e+00, -1.34567536e+00,\n",
       "        -1.33929877e+00],\n",
       "       [-1.74731637e+00, -1.30447998e-01, -1.40117258e+00,\n",
       "        -1.33929877e+00],\n",
       "       [ 2.09677965e+00, -1.30447998e-01,  1.26269381e+00,\n",
       "         1.38501285e+00],\n",
       "       [-1.28136534e+00,  9.04719988e-02, -1.23468093e+00,\n",
       "        -1.33929877e+00],\n",
       "       [-6.98926548e-01,  1.41599198e+00, -1.29017814e+00,\n",
       "        -1.33929877e+00],\n",
       "       [ 1.16487758e-01, -1.89780797e+00,  9.72522648e-02,\n",
       "        -3.01465776e-01],\n",
       "       [-3.49463274e-01, -7.93207989e-01,  2.08246698e-01,\n",
       "         8.77215987e-02],\n",
       "       [-1.63082861e+00, -1.67688798e+00, -1.40117258e+00,\n",
       "        -1.20956965e+00],\n",
       "       [ 1.51434085e+00,  1.19507198e+00,  1.26269381e+00,\n",
       "         1.64447110e+00],\n",
       "       [ 5.82438790e-01, -3.51367995e-01,  2.63743914e-01,\n",
       "         8.77215987e-02],\n",
       "       [-2.32975516e-01, -1.01412799e+00, -1.80233817e-01,\n",
       "        -3.01465776e-01],\n",
       "       [ 2.09677965e+00,  1.63691198e+00,  1.59567711e+00,\n",
       "         1.25528372e+00],\n",
       "       [-1.16487758e+00, -1.23504798e+00,  3.74738347e-01,\n",
       "         6.06638098e-01],\n",
       "       [-1.04838982e+00,  9.74151987e-01, -1.23468093e+00,\n",
       "        -8.20382275e-01],\n",
       "       [ 4.13847651e-15, -1.30447998e-01,  2.08246698e-01,\n",
       "         3.47179848e-01],\n",
       "       [-1.04838982e+00,  9.74151987e-01, -1.40117258e+00,\n",
       "        -1.20956965e+00],\n",
       "       [-1.51434085e+00,  9.04719988e-02, -1.29017814e+00,\n",
       "        -1.33929877e+00],\n",
       "       [ 9.31902064e-01,  9.04719988e-02,  9.85207726e-01,\n",
       "         1.51474197e+00],\n",
       "       [-9.31902064e-01,  1.63691198e+00, -1.06818928e+00,\n",
       "        -1.07984052e+00],\n",
       "       [-2.32975516e-01,  2.96243196e+00, -1.29017814e+00,\n",
       "        -1.07984052e+00],\n",
       "       [-5.82438790e-01,  1.85783197e+00, -1.40117258e+00,\n",
       "        -1.07984052e+00],\n",
       "       [-1.04838982e+00,  7.53231990e-01, -1.23468093e+00,\n",
       "        -1.07984052e+00],\n",
       "       [ 9.31902064e-01,  9.04719988e-02,  3.19241130e-01,\n",
       "         2.17450723e-01],\n",
       "       [ 1.16487758e-01, -3.51367995e-01,  3.74738347e-01,\n",
       "         3.47179848e-01],\n",
       "       [-1.39785310e+00,  3.11391996e-01, -1.23468093e+00,\n",
       "        -1.33929877e+00],\n",
       "       [ 9.31902064e-01, -1.23504798e+00,  1.09620216e+00,\n",
       "         7.36367223e-01],\n",
       "       [-2.32975516e-01, -5.72287992e-01,  1.52749481e-01,\n",
       "         8.77215987e-02],\n",
       "       [-1.16487758e-01, -7.93207989e-01,  7.07721645e-01,\n",
       "         8.66096347e-01],\n",
       "       [ 4.65951032e-01,  5.32311993e-01,  1.20719659e+00,\n",
       "         1.64447110e+00],\n",
       "       [-1.16487758e-01, -1.01412799e+00,  9.72522648e-02,\n",
       "        -4.20075261e-02],\n",
       "       [ 1.51434085e+00, -1.30447998e-01,  1.09620216e+00,\n",
       "         4.76908973e-01],\n",
       "       [ 9.31902064e-01,  9.04719988e-02,  4.85732779e-01,\n",
       "         3.47179848e-01],\n",
       "       [-9.31902064e-01, -1.23504798e+00, -4.57719899e-01,\n",
       "        -1.71736651e-01],\n",
       "       [-1.04838982e+00, -2.33964797e+00, -1.80233817e-01,\n",
       "        -3.01465776e-01],\n",
       "       [ 1.16487758e+00,  3.11391996e-01,  1.04070494e+00,\n",
       "         1.38501285e+00],\n",
       "       [-1.16487758e-01,  2.07875197e+00, -1.45666979e+00,\n",
       "        -1.33929877e+00],\n",
       "       [ 3.49463274e-01, -1.89780797e+00,  3.74738347e-01,\n",
       "         3.47179848e-01],\n",
       "       [-2.32975516e-01, -3.51367995e-01,  2.08246698e-01,\n",
       "         8.77215987e-02],\n",
       "       [ 2.09677965e+00, -1.01412799e+00,  1.70667154e+00,\n",
       "         1.38501285e+00],\n",
       "       [ 4.65951032e-01, -3.51367995e-01,  9.85207726e-01,\n",
       "         7.36367223e-01],\n",
       "       [ 4.65951032e-01, -5.72287992e-01,  7.07721645e-01,\n",
       "         3.47179848e-01],\n",
       "       [ 2.32975516e-01, -5.72287992e-01,  4.85732779e-01,\n",
       "        -4.20075261e-02],\n",
       "       [-3.49463274e-01, -1.30447998e-01,  1.52749481e-01,\n",
       "         8.77215987e-02],\n",
       "       [ 5.82438790e-01, -5.72287992e-01,  9.85207726e-01,\n",
       "         1.12555460e+00],\n",
       "       [ 8.15414306e-01, -1.30447998e-01,  3.19241130e-01,\n",
       "         2.17450723e-01],\n",
       "       [-1.16487758e+00,  9.04719988e-02, -1.29017814e+00,\n",
       "        -1.46902790e+00],\n",
       "       [-1.74731637e+00, -3.51367995e-01, -1.34567536e+00,\n",
       "        -1.33929877e+00],\n",
       "       [ 5.82438790e-01,  9.04719988e-02,  9.29710510e-01,\n",
       "         7.36367223e-01],\n",
       "       [ 5.82438790e-01, -7.93207989e-01,  8.18716077e-01,\n",
       "         8.66096347e-01],\n",
       "       [-9.31902064e-01,  5.32311993e-01, -1.17918371e+00,\n",
       "        -9.50111400e-01],\n",
       "       [-8.15414306e-01,  7.53231990e-01, -1.34567536e+00,\n",
       "        -1.33929877e+00],\n",
       "       [-1.16487758e+00, -1.45596798e+00, -2.91228250e-01,\n",
       "        -3.01465776e-01],\n",
       "       [ 9.31902064e-01, -1.30447998e-01,  6.52224428e-01,\n",
       "         6.06638098e-01],\n",
       "       [ 6.98926548e-01, -1.30447998e-01,  7.63218861e-01,\n",
       "         9.95825472e-01],\n",
       "       [-1.16487758e-01, -7.93207989e-01,  7.07721645e-01,\n",
       "         8.66096347e-01],\n",
       "       [ 1.16487758e+00,  9.04719988e-02,  5.96727212e-01,\n",
       "         3.47179848e-01],\n",
       "       [-5.82438790e-01,  1.85783197e+00, -1.17918371e+00,\n",
       "        -1.07984052e+00],\n",
       "       [-8.15414306e-01, -7.93207989e-01,  4.17550485e-02,\n",
       "         2.17450723e-01],\n",
       "       [-3.49463274e-01, -5.72287992e-01,  5.96727212e-01,\n",
       "         9.95825472e-01],\n",
       "       [ 1.74731637e+00, -5.72287992e-01,  1.26269381e+00,\n",
       "         8.66096347e-01],\n",
       "       [-2.32975516e-01, -1.30447998e-01,  2.08246698e-01,\n",
       "        -4.20075261e-02],\n",
       "       [ 1.63082861e+00, -3.51367995e-01,  1.37368824e+00,\n",
       "         7.36367223e-01],\n",
       "       [ 4.65951032e-01, -1.23504798e+00,  6.52224428e-01,\n",
       "         8.66096347e-01],\n",
       "       [ 1.04838982e+00, -5.72287992e-01,  5.41229996e-01,\n",
       "         2.17450723e-01],\n",
       "       [-1.51434085e+00,  3.11391996e-01, -1.34567536e+00,\n",
       "        -1.33929877e+00],\n",
       "       [-1.16487758e+00,  1.19507198e+00, -1.34567536e+00,\n",
       "        -1.46902790e+00],\n",
       "       [ 1.16487758e-01,  7.53231990e-01,  3.74738347e-01,\n",
       "         4.76908973e-01],\n",
       "       [ 5.82438790e-01, -5.72287992e-01,  9.85207726e-01,\n",
       "         1.25528372e+00],\n",
       "       [-1.04838982e+00,  3.11391996e-01, -1.45666979e+00,\n",
       "        -1.33929877e+00],\n",
       "       [ 3.49463274e-01, -5.72287992e-01,  5.41229996e-01,\n",
       "         7.36367223e-01],\n",
       "       [ 4.13847651e-15, -1.30447998e-01,  7.07721645e-01,\n",
       "         7.36367223e-01],\n",
       "       [-5.82438790e-01,  1.41599198e+00, -1.29017814e+00,\n",
       "        -1.33929877e+00],\n",
       "       [ 5.82438790e-01,  3.11391996e-01,  8.18716077e-01,\n",
       "         1.38501285e+00],\n",
       "       [-5.82438790e-01,  7.53231990e-01, -1.29017814e+00,\n",
       "        -1.07984052e+00],\n",
       "       [ 2.32975516e-01, -3.51367995e-01,  4.85732779e-01,\n",
       "         2.17450723e-01],\n",
       "       [ 2.32975516e-01, -1.01412799e+00,  9.85207726e-01,\n",
       "         2.17450723e-01],\n",
       "       [ 4.65951032e-01, -1.23504798e+00,  5.96727212e-01,\n",
       "         3.47179848e-01]])"
      ]
     },
     "execution_count": 18,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "xtrain_std = StandardScaler().fit_transform(xtrain)\n",
    "xtrain_std"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 20,
   "id": "c2bf7955",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>sepal length (cm)</th>\n",
       "      <th>sepal width (cm)</th>\n",
       "      <th>petal length (cm)</th>\n",
       "      <th>petal width (cm)</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>0</th>\n",
       "      <td>0.698927</td>\n",
       "      <td>0.311392</td>\n",
       "      <td>0.707722</td>\n",
       "      <td>0.995825</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1</th>\n",
       "      <td>0.465951</td>\n",
       "      <td>-1.676888</td>\n",
       "      <td>0.319241</td>\n",
       "      <td>0.087722</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2</th>\n",
       "      <td>-1.164878</td>\n",
       "      <td>0.090472</td>\n",
       "      <td>-1.290178</td>\n",
       "      <td>-1.339299</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>3</th>\n",
       "      <td>1.514341</td>\n",
       "      <td>0.311392</td>\n",
       "      <td>1.207197</td>\n",
       "      <td>0.736367</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>4</th>\n",
       "      <td>-1.164878</td>\n",
       "      <td>-0.130448</td>\n",
       "      <td>-1.345675</td>\n",
       "      <td>-1.339299</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>...</th>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>100</th>\n",
       "      <td>0.582439</td>\n",
       "      <td>0.311392</td>\n",
       "      <td>0.818716</td>\n",
       "      <td>1.385013</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>101</th>\n",
       "      <td>-0.582439</td>\n",
       "      <td>0.753232</td>\n",
       "      <td>-1.290178</td>\n",
       "      <td>-1.079841</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>102</th>\n",
       "      <td>0.232976</td>\n",
       "      <td>-0.351368</td>\n",
       "      <td>0.485733</td>\n",
       "      <td>0.217451</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>103</th>\n",
       "      <td>0.232976</td>\n",
       "      <td>-1.014128</td>\n",
       "      <td>0.985208</td>\n",
       "      <td>0.217451</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>104</th>\n",
       "      <td>0.465951</td>\n",
       "      <td>-1.235048</td>\n",
       "      <td>0.596727</td>\n",
       "      <td>0.347180</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "<p>105 rows × 4 columns</p>\n",
       "</div>"
      ],
      "text/plain": [
       "     sepal length (cm)  sepal width (cm)  petal length (cm)  petal width (cm)\n",
       "0             0.698927          0.311392           0.707722          0.995825\n",
       "1             0.465951         -1.676888           0.319241          0.087722\n",
       "2            -1.164878          0.090472          -1.290178         -1.339299\n",
       "3             1.514341          0.311392           1.207197          0.736367\n",
       "4            -1.164878         -0.130448          -1.345675         -1.339299\n",
       "..                 ...               ...                ...               ...\n",
       "100           0.582439          0.311392           0.818716          1.385013\n",
       "101          -0.582439          0.753232          -1.290178         -1.079841\n",
       "102           0.232976         -0.351368           0.485733          0.217451\n",
       "103           0.232976         -1.014128           0.985208          0.217451\n",
       "104           0.465951         -1.235048           0.596727          0.347180\n",
       "\n",
       "[105 rows x 4 columns]"
      ]
     },
     "execution_count": 20,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "xtrain_std = pd.DataFrame(data=xtrain_std,columns=load_iris().feature_names)\n",
    "xtrain_std"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 22,
   "id": "6141d3f1",
   "metadata": {
    "scrolled": true
   },
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>sepal length (cm)</th>\n",
       "      <th>sepal width (cm)</th>\n",
       "      <th>petal length (cm)</th>\n",
       "      <th>petal width (cm)</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>0</th>\n",
       "      <td>-0.153220</td>\n",
       "      <td>-0.137280</td>\n",
       "      <td>0.545585</td>\n",
       "      <td>0.519487</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1</th>\n",
       "      <td>1.087862</td>\n",
       "      <td>-0.137280</td>\n",
       "      <td>1.335604</td>\n",
       "      <td>1.482065</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2</th>\n",
       "      <td>2.604741</td>\n",
       "      <td>-0.137280</td>\n",
       "      <td>1.821769</td>\n",
       "      <td>1.344554</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>3</th>\n",
       "      <td>-1.394302</td>\n",
       "      <td>0.377519</td>\n",
       "      <td>-1.399075</td>\n",
       "      <td>-1.268159</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>4</th>\n",
       "      <td>-0.980608</td>\n",
       "      <td>0.892318</td>\n",
       "      <td>-1.277534</td>\n",
       "      <td>-1.268159</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>5</th>\n",
       "      <td>0.122576</td>\n",
       "      <td>-0.909478</td>\n",
       "      <td>0.302503</td>\n",
       "      <td>-0.168069</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>6</th>\n",
       "      <td>-0.704812</td>\n",
       "      <td>1.149717</td>\n",
       "      <td>-1.277534</td>\n",
       "      <td>-1.268159</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>7</th>\n",
       "      <td>-0.291118</td>\n",
       "      <td>-1.166877</td>\n",
       "      <td>0.484815</td>\n",
       "      <td>0.106953</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>8</th>\n",
       "      <td>0.398372</td>\n",
       "      <td>-0.137280</td>\n",
       "      <td>0.727897</td>\n",
       "      <td>0.932020</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>9</th>\n",
       "      <td>0.536270</td>\n",
       "      <td>-0.137280</td>\n",
       "      <td>0.606356</td>\n",
       "      <td>0.381976</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>10</th>\n",
       "      <td>1.639455</td>\n",
       "      <td>0.120120</td>\n",
       "      <td>1.092521</td>\n",
       "      <td>1.344554</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>11</th>\n",
       "      <td>-0.429016</td>\n",
       "      <td>-0.137280</td>\n",
       "      <td>0.545585</td>\n",
       "      <td>0.519487</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>12</th>\n",
       "      <td>-1.532200</td>\n",
       "      <td>1.407116</td>\n",
       "      <td>-1.581387</td>\n",
       "      <td>-1.268159</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>13</th>\n",
       "      <td>-0.980608</td>\n",
       "      <td>-0.137280</td>\n",
       "      <td>-1.216763</td>\n",
       "      <td>-1.268159</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>14</th>\n",
       "      <td>1.501556</td>\n",
       "      <td>-0.137280</td>\n",
       "      <td>1.153292</td>\n",
       "      <td>1.344554</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>15</th>\n",
       "      <td>0.122576</td>\n",
       "      <td>-0.909478</td>\n",
       "      <td>0.180961</td>\n",
       "      <td>0.106953</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>16</th>\n",
       "      <td>-1.807997</td>\n",
       "      <td>0.377519</td>\n",
       "      <td>-1.399075</td>\n",
       "      <td>-1.268159</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>17</th>\n",
       "      <td>-1.532200</td>\n",
       "      <td>0.892318</td>\n",
       "      <td>-1.338305</td>\n",
       "      <td>-1.130648</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>18</th>\n",
       "      <td>-1.256404</td>\n",
       "      <td>0.892318</td>\n",
       "      <td>-1.216763</td>\n",
       "      <td>-1.268159</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>19</th>\n",
       "      <td>-0.842710</td>\n",
       "      <td>1.921915</td>\n",
       "      <td>-1.216763</td>\n",
       "      <td>-1.268159</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>20</th>\n",
       "      <td>-0.429016</td>\n",
       "      <td>0.892318</td>\n",
       "      <td>-1.155993</td>\n",
       "      <td>-1.268159</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>21</th>\n",
       "      <td>1.363658</td>\n",
       "      <td>-0.137280</td>\n",
       "      <td>0.970980</td>\n",
       "      <td>1.619576</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>22</th>\n",
       "      <td>-0.291118</td>\n",
       "      <td>-1.424276</td>\n",
       "      <td>0.241732</td>\n",
       "      <td>0.244464</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>23</th>\n",
       "      <td>-1.256404</td>\n",
       "      <td>-0.137280</td>\n",
       "      <td>-1.338305</td>\n",
       "      <td>-1.405670</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>24</th>\n",
       "      <td>1.087862</td>\n",
       "      <td>-0.652078</td>\n",
       "      <td>0.606356</td>\n",
       "      <td>0.519487</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>25</th>\n",
       "      <td>0.812066</td>\n",
       "      <td>0.634918</td>\n",
       "      <td>0.667127</td>\n",
       "      <td>0.656998</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>26</th>\n",
       "      <td>-0.842710</td>\n",
       "      <td>0.892318</td>\n",
       "      <td>-1.277534</td>\n",
       "      <td>-1.268159</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>27</th>\n",
       "      <td>-0.291118</td>\n",
       "      <td>2.951512</td>\n",
       "      <td>-1.338305</td>\n",
       "      <td>-1.268159</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>28</th>\n",
       "      <td>-0.291118</td>\n",
       "      <td>-1.681676</td>\n",
       "      <td>0.059420</td>\n",
       "      <td>-0.168069</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>29</th>\n",
       "      <td>1.225760</td>\n",
       "      <td>-0.394679</td>\n",
       "      <td>0.606356</td>\n",
       "      <td>0.244464</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>30</th>\n",
       "      <td>-0.980608</td>\n",
       "      <td>-1.939075</td>\n",
       "      <td>-0.183662</td>\n",
       "      <td>-0.168069</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>31</th>\n",
       "      <td>0.812066</td>\n",
       "      <td>-0.909478</td>\n",
       "      <td>0.788668</td>\n",
       "      <td>0.932020</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>32</th>\n",
       "      <td>1.363658</td>\n",
       "      <td>0.634918</td>\n",
       "      <td>1.274833</td>\n",
       "      <td>1.344554</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>33</th>\n",
       "      <td>-0.980608</td>\n",
       "      <td>0.634918</td>\n",
       "      <td>-1.338305</td>\n",
       "      <td>-1.268159</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>34</th>\n",
       "      <td>-0.842710</td>\n",
       "      <td>1.149717</td>\n",
       "      <td>-1.338305</td>\n",
       "      <td>-1.130648</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>35</th>\n",
       "      <td>0.949964</td>\n",
       "      <td>0.377519</td>\n",
       "      <td>0.545585</td>\n",
       "      <td>0.519487</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>36</th>\n",
       "      <td>0.812066</td>\n",
       "      <td>0.892318</td>\n",
       "      <td>1.214063</td>\n",
       "      <td>1.757087</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>37</th>\n",
       "      <td>-0.153220</td>\n",
       "      <td>-0.394679</td>\n",
       "      <td>-0.001350</td>\n",
       "      <td>0.244464</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>38</th>\n",
       "      <td>0.260474</td>\n",
       "      <td>0.377519</td>\n",
       "      <td>0.727897</td>\n",
       "      <td>0.932020</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>39</th>\n",
       "      <td>-0.015322</td>\n",
       "      <td>-0.652078</td>\n",
       "      <td>0.545585</td>\n",
       "      <td>0.244464</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>40</th>\n",
       "      <td>-0.291118</td>\n",
       "      <td>-1.681676</td>\n",
       "      <td>0.120191</td>\n",
       "      <td>-0.030558</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>41</th>\n",
       "      <td>0.674168</td>\n",
       "      <td>-0.394679</td>\n",
       "      <td>0.424044</td>\n",
       "      <td>0.244464</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>42</th>\n",
       "      <td>-0.291118</td>\n",
       "      <td>-1.939075</td>\n",
       "      <td>0.241732</td>\n",
       "      <td>0.244464</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>43</th>\n",
       "      <td>0.398372</td>\n",
       "      <td>-0.909478</td>\n",
       "      <td>0.910209</td>\n",
       "      <td>0.656998</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>44</th>\n",
       "      <td>1.087862</td>\n",
       "      <td>-0.137280</td>\n",
       "      <td>1.153292</td>\n",
       "      <td>0.932020</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "    sepal length (cm)  sepal width (cm)  petal length (cm)  petal width (cm)\n",
       "0           -0.153220         -0.137280           0.545585          0.519487\n",
       "1            1.087862         -0.137280           1.335604          1.482065\n",
       "2            2.604741         -0.137280           1.821769          1.344554\n",
       "3           -1.394302          0.377519          -1.399075         -1.268159\n",
       "4           -0.980608          0.892318          -1.277534         -1.268159\n",
       "5            0.122576         -0.909478           0.302503         -0.168069\n",
       "6           -0.704812          1.149717          -1.277534         -1.268159\n",
       "7           -0.291118         -1.166877           0.484815          0.106953\n",
       "8            0.398372         -0.137280           0.727897          0.932020\n",
       "9            0.536270         -0.137280           0.606356          0.381976\n",
       "10           1.639455          0.120120           1.092521          1.344554\n",
       "11          -0.429016         -0.137280           0.545585          0.519487\n",
       "12          -1.532200          1.407116          -1.581387         -1.268159\n",
       "13          -0.980608         -0.137280          -1.216763         -1.268159\n",
       "14           1.501556         -0.137280           1.153292          1.344554\n",
       "15           0.122576         -0.909478           0.180961          0.106953\n",
       "16          -1.807997          0.377519          -1.399075         -1.268159\n",
       "17          -1.532200          0.892318          -1.338305         -1.130648\n",
       "18          -1.256404          0.892318          -1.216763         -1.268159\n",
       "19          -0.842710          1.921915          -1.216763         -1.268159\n",
       "20          -0.429016          0.892318          -1.155993         -1.268159\n",
       "21           1.363658         -0.137280           0.970980          1.619576\n",
       "22          -0.291118         -1.424276           0.241732          0.244464\n",
       "23          -1.256404         -0.137280          -1.338305         -1.405670\n",
       "24           1.087862         -0.652078           0.606356          0.519487\n",
       "25           0.812066          0.634918           0.667127          0.656998\n",
       "26          -0.842710          0.892318          -1.277534         -1.268159\n",
       "27          -0.291118          2.951512          -1.338305         -1.268159\n",
       "28          -0.291118         -1.681676           0.059420         -0.168069\n",
       "29           1.225760         -0.394679           0.606356          0.244464\n",
       "30          -0.980608         -1.939075          -0.183662         -0.168069\n",
       "31           0.812066         -0.909478           0.788668          0.932020\n",
       "32           1.363658          0.634918           1.274833          1.344554\n",
       "33          -0.980608          0.634918          -1.338305         -1.268159\n",
       "34          -0.842710          1.149717          -1.338305         -1.130648\n",
       "35           0.949964          0.377519           0.545585          0.519487\n",
       "36           0.812066          0.892318           1.214063          1.757087\n",
       "37          -0.153220         -0.394679          -0.001350          0.244464\n",
       "38           0.260474          0.377519           0.727897          0.932020\n",
       "39          -0.015322         -0.652078           0.545585          0.244464\n",
       "40          -0.291118         -1.681676           0.120191         -0.030558\n",
       "41           0.674168         -0.394679           0.424044          0.244464\n",
       "42          -0.291118         -1.939075           0.241732          0.244464\n",
       "43           0.398372         -0.909478           0.910209          0.656998\n",
       "44           1.087862         -0.137280           1.153292          0.932020"
      ]
     },
     "execution_count": 22,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "xtest_std = StandardScaler().fit_transform(xtest)\n",
    "xtest_std = pd.DataFrame(xtest_std,columns=load_iris().feature_names)\n",
    "xtest_std"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "5eb8db5c",
   "metadata": {},
   "source": [
    "### 在确定l2范式的情况下，使用网格搜索判断solver, C的最优组合"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 23,
   "id": "b7ac1f2b",
   "metadata": {},
   "outputs": [
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "C:\\Users\\LouisLou\\AppData\\Local\\Programs\\Python\\Python39\\lib\\site-packages\\sklearn\\model_selection\\_validation.py:372: FitFailedWarning: \n",
      "95 fits failed out of a total of 380.\n",
      "The score on these train-test partitions for these parameters will be set to nan.\n",
      "If these failures are not expected, you can try to debug them by setting error_score='raise'.\n",
      "\n",
      "Below are more details about the failures:\n",
      "--------------------------------------------------------------------------------\n",
      "95 fits failed with the following error:\n",
      "Traceback (most recent call last):\n",
      "  File \"C:\\Users\\LouisLou\\AppData\\Local\\Programs\\Python\\Python39\\lib\\site-packages\\sklearn\\model_selection\\_validation.py\", line 680, in _fit_and_score\n",
      "    estimator.fit(X_train, y_train, **fit_params)\n",
      "  File \"C:\\Users\\LouisLou\\AppData\\Local\\Programs\\Python\\Python39\\lib\\site-packages\\sklearn\\linear_model\\_logistic.py\", line 1461, in fit\n",
      "    solver = _check_solver(self.solver, self.penalty, self.dual)\n",
      "  File \"C:\\Users\\LouisLou\\AppData\\Local\\Programs\\Python\\Python39\\lib\\site-packages\\sklearn\\linear_model\\_logistic.py\", line 434, in _check_solver\n",
      "    raise ValueError(\n",
      "ValueError: Logistic Regression supports only solvers in ['liblinear', 'newton-cg', 'lbfgs', 'sag', 'saga'], got Ibfgs.\n",
      "\n",
      "  warnings.warn(some_fits_failed_message, FitFailedWarning)\n",
      "C:\\Users\\LouisLou\\AppData\\Local\\Programs\\Python\\Python39\\lib\\site-packages\\sklearn\\model_selection\\_search.py:969: UserWarning: One or more of the test scores are non-finite: [0.86666667        nan 0.79047619 0.86666667 0.8952381         nan\n",
      " 0.80952381 0.8952381  0.91428571        nan 0.82857143 0.91428571\n",
      " 0.94285714        nan 0.85714286 0.94285714 0.95238095        nan\n",
      " 0.86666667 0.95238095 0.95238095        nan 0.87619048 0.95238095\n",
      " 0.96190476        nan 0.87619048 0.96190476 0.97142857        nan\n",
      " 0.87619048 0.97142857 0.96190476        nan 0.8952381  0.96190476\n",
      " 0.96190476        nan 0.8952381  0.96190476 0.96190476        nan\n",
      " 0.8952381  0.96190476 0.96190476        nan 0.8952381  0.96190476\n",
      " 0.97142857        nan 0.8952381  0.97142857 0.97142857        nan\n",
      " 0.8952381  0.97142857 0.97142857        nan 0.8952381  0.97142857\n",
      " 0.97142857        nan 0.8952381  0.97142857 0.97142857        nan\n",
      " 0.8952381  0.97142857 0.97142857        nan 0.8952381  0.97142857\n",
      " 0.97142857        nan 0.8952381  0.97142857]\n",
      "  warnings.warn(\n"
     ]
    },
    {
     "data": {
      "text/plain": [
       "0.9714285714285715"
      ]
     },
     "execution_count": 23,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "p = {\n",
    "    'C':list(np.linspace(0.05,1,19)),\n",
    "    'solver':['newton-cg','Ibfgs','liblinear','sag']\n",
    "}\n",
    "model = LogisticRegression(penalty='l2',max_iter=1000)\n",
    "GS = GridSearchCV(model,p,cv=5).fit(xtrain_std,ytrain)\n",
    "GS.best_score_"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 24,
   "id": "c12c77b6",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "{'C': 0.41944444444444445, 'solver': 'newton-cg'}"
      ]
     },
     "execution_count": 24,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "GS.best_params_"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "a77e2176",
   "metadata": {},
   "source": [
    "### 将最优的结果重新用来实例化模型，查看训练集和测试集下的分数"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 28,
   "id": "747631a7",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "0.9904761904761905"
      ]
     },
     "execution_count": 28,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "lrl2 = LogisticRegression(penalty='l2',C=GS.best_params_['C'],solver=GS.best_params_['solver'],max_iter=1000)\n",
    "lrl2 = lrl2.fit(xtrain,ytrain)\n",
    "train_score = lrl2.score(xtrain,ytrain)\n",
    "train_score"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 29,
   "id": "2aafac26",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "0.9555555555555556"
      ]
     },
     "execution_count": 29,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "lrl2 = LogisticRegression(penalty='l2',C=GS.best_params_['C'],solver=GS.best_params_['solver'],max_iter=1000)\n",
    "lrl2 = lrl2.fit(xtest,ytest)\n",
    "test_score = lrl2.score(xtest,ytest)\n",
    "test_score"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "4b6e19e8",
   "metadata": {},
   "source": [
    "### 计算精准率"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 30,
   "id": "4fc65374",
   "metadata": {},
   "outputs": [],
   "source": [
    "from sklearn.metrics import precision_score"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 33,
   "id": "26ed516f",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "0.9555555555555556"
      ]
     },
     "execution_count": 33,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "pre_score = precision_score(y_true=ytest,y_pred=lrl2.predict(xtest),average='micro')\n",
    "pre_score"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "9db25c4b",
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3 (ipykernel)",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.9.7"
  },
  "toc": {
   "base_numbering": 1,
   "nav_menu": {},
   "number_sections": true,
   "sideBar": true,
   "skip_h1_title": false,
   "title_cell": "Table of Contents",
   "title_sidebar": "Contents",
   "toc_cell": false,
   "toc_position": {},
   "toc_section_display": true,
   "toc_window_display": false
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
